package com.softeem.oss_music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.oss_music.comm.R;
import com.softeem.oss_music.entity.Album;
import com.softeem.oss_music.service.AlbumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "专辑管理")
@RestController
public class AlbumController {
    /**
     * 专辑管理：专辑列表、专辑详情、添加专辑、修改专辑、删除专辑
     */
    @Autowired
    private AlbumService albumService;
    
    @Operation(summary = "专辑列表", description = "根据提供的页码和数据链条实现分页查询")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "数据条数")
    })
    @GetMapping("/album/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit){
        // 创建分页对象
        IPage<Album> iPage = new Page<>(page, limit);
        // 调用page方法进行分页查询
        iPage = albumService.page(iPage);
        // 从分页对象中获取数据和总数
        List<Album> list = iPage.getRecords();
        long count = iPage.getTotal();
        return R.success().setData(list).setCount(count);
    }
    
    @Operation(summary = "所有专辑列表", description = "查询所有专辑")
    @GetMapping("/album/listAll")
    public R listAll(){
        return R.success().setData(albumService.list());
    }
    
    @Operation(summary = "专辑详情", description = "根据专辑ID查询专辑详情")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID")
    })
    @GetMapping("/album/details")
    public R details(Integer id){
        if (id == null) {
            return R.error().setMessage("专辑ID不能为空");
        }
        Album album = albumService.getById(id);
        if (album == null) {
            return R.error().setMessage("专辑不存在");
        }
        return R.success().setData(album);
    }
    
    @Operation(summary = "添加专辑", description = "添加新专辑")
    @PostMapping("/album/add")
    public R add(@RequestBody Album album){
        if (album == null) {
            return R.error().setMessage("专辑信息不能为空");
        }
        if (album.getName() == null || album.getName().trim().isEmpty()) {
            return R.error().setMessage("专辑名称不能为空");
        }
        // 设置默认值
        if (album.getPublishTime() == null) {
            album.setPublishTime(new Date());
        }
        if (album.getStatus() == null) {
            album.setStatus(1); // 默认上架
        }
        boolean result = albumService.save(album);
        return result ? R.success().setMessage("添加成功").setData(album) : R.error().setMessage("添加失败");
    }
    
    @Operation(summary = "更新专辑", description = "更新专辑信息")
    @PostMapping("/album/update")
    public R update(@RequestBody Album album){
        if (album == null || album.getId() == null) {
            return R.error().setMessage("专辑ID不能为空");
        }
        if (album.getName() == null || album.getName().trim().isEmpty()) {
            return R.error().setMessage("专辑名称不能为空");
        }
        Album existingAlbum = albumService.getById(album.getId());
        if (existingAlbum == null) {
            return R.error().setMessage("专辑不存在");
        }
        boolean result = albumService.updateById(album);
        return result ? R.success().setMessage("更新成功").setData(album) : R.error().setMessage("更新失败");
    }
    
    @Operation(summary = "删除专辑", description = "根据专辑ID删除专辑")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID")
    })
    @DeleteMapping("/album/delete")
    public R delete(Integer id){
        if (id == null) {
            return R.error().setMessage("专辑ID不能为空");
        }
        // 检查专辑是否存在
        Album album = albumService.getById(id);
        if (album == null) {
            return R.error().setMessage("专辑不存在");
        }
        // 这里可以添加检查专辑中是否有歌曲的逻辑
        boolean result = albumService.removeById(id);
        return result ? R.success().setMessage("删除成功") : R.error().setMessage("删除失败");
    }
}
